﻿@page
@using RazorPagesSample.Models
@inject BooksContext _context
@{
    ViewData["Title"] = "Inline";
}

@functions
{
    public void OnGet()
    {
        bool created = _context.Database.EnsureCreated();
        if (created) SeedBooks();

        Books = _context.Books.ToList();
    }

    public IEnumerable<Book> Books { get; set; }

    private void SeedBooks()
    {
        _context.Books.Add(new Book { Title = "Professional C# 6 and .NET Core 1", Publisher = "Wrox Press" });
        _context.Books.Add(new Book { Title = "Professional C# 7 and .NET Core 2", Publisher = "Wrox Press" });
        _context.SaveChanges();
    }

    public void OnPost()
    {
        _context.Books.Add(Book);
        _context.SaveChanges();
        Message = "Book saved";
        Books = _context.Books.ToList();
    }

    [BindProperty()]
    public Book Book { get; set; }

    public string Message { get; set; } = string.Empty;
}

<h2>Inline Razor Page Sample</h2>

@if (Books != null)
{
    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Publisher</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var book in Books)
            {
                <tr>
                    <td>@book.Title</td>
                    <td>@book.Publisher</td>
                </tr>
            }
        </tbody>
    </table>
}

<div>@Message</div>

<form method="post">
    Enter a new book
    <br />
    <input type="text" name="Title" id="Title" />
    <br />
    <input type="text" name="Publisher" id="Publisher" />
    <br />
    <button type="submit">Submit</button>
</form>

